<meta name="csrf-token" content="{{ csrf_token() }}">
<div class="box box-info">
    <div class="box-header with-border">
        <h3 class="box-title">{{ $logistics->name }}</h3>
        <div class="box-tools">
            <div class="btn-group float-right" style="margin-right: 10px">
                <a href="{{ route('admin.logistics.index') }}" class="btn btn-sm btn-default"><i class="fa fa-list"></i>
                    列表</a>
            </div>
        </div>
    </div>
    <div class="box-body">
        @foreach($logistics->templates as $value)
            <div class="card">
                <div class="card-header">
                    {{$value->name}} (配送范围: {{\App\Models\LogisticsTemplate::$typeMap[$value->type]}})
                    <div class="left">
                        <button type="button" class="btn btn-link btn-sm btn-logistics-template-delete" data-url="{{ route('admin.logistics-templates.destroy', ['logistics_template' => $value->id])}}">删除</button>
                    </div>
                </div>
                <div class="card-body">
                    <table class="table table-bordered">
                        <tbody>
                        <tr>
                            <td class="align-middle">按件数</td>
                            <td class="align-middle">首件(件)：{{$value->first_piece}}</td>
                            <td class="align-middle">首件运费(元)：{{ $value->first_piece_price }}</td>
                            <td class="align-middle">续件(件)：{{ $value->other_piece }}</td>
                            <td class="align-middle">续件运费(元): {{ $value->other_piece_price }}</td>
                            <td class="align-middle">
                                @if($value->open_piece)
                                    <button type="button" class="btn btn-success btn-sm btn-status-logistics" data-type="piece" data-url="{{ route('admin.logistics-templates.status', ['logistics_template' => $value->id])}}">启动</button>
                                @else
                                    <button type="button" class="btn btn-danger btn-sm btn-status-logistics" data-type="piece" data-url="{{ route('admin.logistics-templates.status', ['logistics_template' => $value->id])}}">关闭</button>
                                @endif
                                <button type="button" class="btn btn-info btn-sm btn-edit-logistics" data-url="{{ route('admin.logistics-templates.edit', ['logistics_template' => $value->id]) }}">编辑</button>
                            </td>
                        </tr>
                        <tr>
                            <td class="align-middle">按重量</td>
                            <td class="align-middle">首重(kg)：{{$value->first_weight}}</td>
                            <td class="align-middle">首重运费(元)：{{ $value->first_weight_price }}</td>
                            <td class="align-middle">续重(kg)：{{ $value->other_weight }}</td>
                            <td class="align-middle">续重运费(元): {{ $value->other_weight_price }}</td>
                            <td class="align-middle">
                                @if($value->open_weight)
                                    <button type="button" class="btn btn-success btn-sm btn-status-logistics" data-type="weight" data-url="{{ route('admin.logistics-templates.status', ['logistics_template' => $value->id])}}">启动</button>
                                @else
                                    <button type="button" class="btn btn-danger btn-sm btn-status-logistics" data-type="weight" data-url="{{ route('admin.logistics-templates.status', ['logistics_template' => $value->id])}}">关闭</button>
                                @endif
                                <button type="button" class="btn btn-info btn-sm btn-edit-logistics" data-url="{{ route('admin.logistics-templates.edit', ['logistics_template' => $value->id]) }}">编辑</button>
                            </td>
                        </tr>
                        <tr>
                            <td class="align-middle">按体积</td>
                            <td class="align-middle">首体积(m³)：{{$value->first_volume}}</td>
                            <td class="align-middle">首体积运费(元)：{{ $value->first_volume_price }}</td>
                            <td class="align-middle">续体积(m³)：{{ $value->other_volume }}</td>
                            <td class="align-middle">续体积运费(元): {{ $value->other_volume_price }}</td>
                            <td class="align-middle">
                                @if($value->open_volume)
                                    <button type="button" class="btn btn-success btn-sm btn-status-logistics" data-type="volume" data-url="{{ route('admin.logistics-templates.status', ['logistics_template' => $value->id])}}">启动</button>
                                @else
                                    <button type="button" class="btn btn-danger btn-sm btn-status-logistics" data-type="volume" data-url="{{ route('admin.logistics-templates.status', ['logistics_template' => $value->id])}}">关闭</button>
                                @endif
                                <button type="button" class="btn btn-info btn-sm btn-edit-logistics" data-url="{{ route('admin.logistics-templates.edit', ['logistics_template' => $value->id]) }}">编辑</button>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        @endforeach
    </div>
</div>

<script>
    Dcat.ready(function () {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        // 删除按钮
        $(".btn-logistics-template-delete").click(function () {
            let url = $(this).data('url');
            Dcat.confirm('你确定要删除本模板吗?', null,
                function () {
                    $.ajax({
                        url: url,
                        method: "DELETE",
                        contentType: 'application/json'
                    });
                    location.reload();
                }, function () {});
        });

        // 编辑按钮
        $(".btn-edit-logistics").click(function () {
            let url = $(this).data('url');
            location.href = url;
        })

        // status
        $('.btn-status-logistics').click(function () {
            let url = $(this).data('url');
            let type = $(this).data('type');
            $.ajax({
                url: url,
                method: "post",
                data: JSON.stringify({
                   type: type
                }),
                contentType: "application/json"
            }).then(function () {
                location.reload();
            }).catch(function () {

            })
        })
    });
</script>
